﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Threading;
using VietImage.Logs;

namespace VietImage.Models
{
    public class Provider
    {
        public static Boolean Insert(ref System.Object ob, String tableName)
        {
            //VietImageDB_Log.WriteLog("Begin Insert.");
            VietImageDataClassesDataContext dbProvider = new VietImageDataClassesDataContext();

            switch (tableName)
            {
                case "Actions":
                    {
                        Action ii = (Action)ob;
                        dbProvider.Actions.InsertOnSubmit(ii);
                        break;
                    }
                case "Categories":
                    {
                        Category ii = (Category)ob;
                        dbProvider.Categories.InsertOnSubmit(ii);
                        break;
                    }
                case "Cities":
                    {
                        City ii = (City)ob;
                        dbProvider.Cities.InsertOnSubmit(ii);
                        break;
                    }
                case "Countries":
                    {
                        Country ii = (Country)ob;
                        dbProvider.Countries.InsertOnSubmit(ii);
                        break;
                    }
                case "Customers":
                    {
                        Customer ii = (Customer)ob;
                        dbProvider.Customers.InsertOnSubmit(ii);
                        break;
                    }
                case "DetailCategories":
                    {
                        DetailCategory ii = (DetailCategory)ob;
                        dbProvider.DetailCategories.InsertOnSubmit(ii);
                        break;
                    }
                case "DetailCustomers":
                    {
                        DetailCustomer ii = (DetailCustomer)ob;
                        dbProvider.DetailCustomers.InsertOnSubmit(ii);
                        break;
                    }
                case "DetailImages":
                    {
                        DetailImage ii = (DetailImage)ob;
                        dbProvider.DetailImages.InsertOnSubmit(ii);
                        break;
                    }
                case "DetailInfos":
                    {
                        DetailInfo ii = (DetailInfo)ob;
                        dbProvider.DetailInfos.InsertOnSubmit(ii);
                        break;
                    }
                case "DetailUsers":
                    {
                        DetailUser ii = (DetailUser)ob;
                        dbProvider.DetailUsers.InsertOnSubmit(ii);
                        break;
                    }
                case "Emails":
                    {
                        Email ii = (Email)ob;
                        dbProvider.Emails.InsertOnSubmit(ii);
                        break;
                    }
                case "ErrorMessages":
                    {
                        ErrorMessage ii = (ErrorMessage)ob;
                        dbProvider.ErrorMessages.InsertOnSubmit(ii);
                        break;
                    }
                case "Images":
                    {
                        Image ii = (Image)ob;
                        dbProvider.Images.InsertOnSubmit(ii);
                        break;
                    }
                case "ImageLightboxes":
                    {
                        ImageLightbox ii = (ImageLightbox)ob;
                        dbProvider.ImageLightboxes.InsertOnSubmit(ii);
                        break;
                    }
                case "Infos":
                    {
                        Info ii = (Info)ob;
                        dbProvider.Infos.InsertOnSubmit(ii);
                        break;
                    }
                case "Languages":
                    {
                        Language ii = (Language)ob;
                        dbProvider.Languages.InsertOnSubmit(ii);
                        break;
                    }
                case "Lightboxes":
                    {
                        Lightbox ii = (Lightbox)ob;
                        dbProvider.Lightboxes.InsertOnSubmit(ii);
                        break;
                    }
                case "LogCategories":
                    {
                        LogCategory ii = (LogCategory)ob;
                        dbProvider.LogCategories.InsertOnSubmit(ii);
                        break;
                    }
                case "LogCustomers":
                    {
                        LogCustomer ii = (LogCustomer)ob;
                        dbProvider.LogCustomers.InsertOnSubmit(ii);
                        break;
                    }
                case "LogEmails":
                    {
                        LogEmail ii = (LogEmail)ob;
                        dbProvider.LogEmails.InsertOnSubmit(ii);
                        break;
                    }
                case "LogImages":
                    {
                        LogImage ii = (LogImage)ob;
                        dbProvider.LogImages.InsertOnSubmit(ii);
                        break;
                    }
                case "LogImageCategories":
                    {
                        LogImageCategory ii = (LogImageCategory)ob;
                        dbProvider.LogImageCategories.InsertOnSubmit(ii);
                        break;
                    }
                case "LogImageLightboxes":
                    {
                        LogImageLightbox ii = (LogImageLightbox)ob;
                        dbProvider.LogImageLightboxes.InsertOnSubmit(ii);
                        break;
                    }
                case "LogInfos":
                    {
                        LogInfo ii = (LogInfo)ob;
                        dbProvider.LogInfos.InsertOnSubmit(ii);
                        break;
                    }
                case "LogLightboxes":
                    {
                        LogLightbox ii = (LogLightbox)ob;
                        dbProvider.LogLightboxes.InsertOnSubmit(ii);
                        break;
                    }
                case "LogNoticeEmails":
                    {
                        LogNoticeEmail ii = (LogNoticeEmail)ob;
                        dbProvider.LogNoticeEmails.InsertOnSubmit(ii);
                        break;
                    }
                case "LogPayments":
                    {
                        LogPayment ii = (LogPayment)ob;
                        dbProvider.LogPayments.InsertOnSubmit(ii);
                        break;
                    }
                case "LogRecentActivities":
                    {
                        LogRecentActivity ii = (LogRecentActivity)ob;
                        dbProvider.LogRecentActivities.InsertOnSubmit(ii);
                        break;
                    }
                case "LogRequests":
                    {
                        LogRequest ii = (LogRequest)ob;
                        dbProvider.LogRequests.InsertOnSubmit(ii);
                        break;
                    }
                case "LogSubscribers":
                    {
                        LogSubscriber ii = (LogSubscriber)ob;
                        dbProvider.LogSubscribers.InsertOnSubmit(ii);
                        break;
                    }
                case "LogTranslations":
                    {
                        LogTranslation ii = (LogTranslation)ob;
                        dbProvider.LogTranslations.InsertOnSubmit(ii);
                        break;
                    }
                case "LogUsers":
                    {
                        LogUser ii = (LogUser)ob;
                        dbProvider.LogUsers.InsertOnSubmit(ii);
                        break;
                    }
                case "NoticeEmails":
                    {
                        NoticeEmail ii = (NoticeEmail)ob;
                        dbProvider.NoticeEmails.InsertOnSubmit(ii);
                        break;
                    }
                case "Payments":
                    {
                        Payment ii = (Payment)ob;
                        dbProvider.Payments.InsertOnSubmit(ii);
                        break;
                    }
                case "Permissions":
                    {
                        Permission ii = (Permission)ob;
                        dbProvider.Permissions.InsertOnSubmit(ii);
                        break;
                    }
                case "Objects":
                    {
                        Object ii = (Object)ob;
                        dbProvider.Objects.InsertOnSubmit(ii);
                        break;
                    }
                case "Status":
                    {
                        Status ii = (Status)ob;
                        dbProvider.Status.InsertOnSubmit(ii);
                        break;
                    }
                case "Subscribers":
                    {
                        Subscriber ii = (Subscriber)ob;
                        dbProvider.Subscribers.InsertOnSubmit(ii);
                        break;
                    }
                case "Users":
                    {
                        User ii = (User)ob;
                        dbProvider.Users.InsertOnSubmit(ii);
                        break;
                    }
            }

            dbProvider.SubmitChanges();

            if (TrySaveChange(dbProvider, 5))
            {
                //VietImageDB_Log.WriteLog("Successed!");
                //VietImageDB_Log.WriteLog("End Insert.");
                return true;
            }
            //VietImageDB_Log.WriteLog("Failed!");
            //VietImageDB_Log.WriteLog("End Insert.");
            return false;
        }

        public static Boolean Delete(System.Object ob, String tableName)
        {
            VietImageDB_Log.WriteLog("Begin Delete.");
            VietImageDataClassesDataContext dbProvider = new VietImageDataClassesDataContext();

            switch (tableName)
            {
                case "Actions":
                    {
                        Action ii = (Action)ob;
                        dbProvider.Actions.DeleteOnSubmit(ii);
                        break;
                    }
                case "Categories":
                    {
                        Category ii = (Category)ob;
                        dbProvider.Categories.DeleteOnSubmit(ii);
                        break;
                    }
                case "Cities":
                    {
                        City ii = (City)ob;
                        dbProvider.Cities.DeleteOnSubmit(ii);
                        break;
                    }
                case "Countries":
                    {
                        Country ii = (Country)ob;
                        dbProvider.Countries.DeleteOnSubmit(ii);
                        break;
                    }
                case "Customers":
                    {
                        Customer ii = (Customer)ob;
                        dbProvider.Customers.DeleteOnSubmit(ii);
                        break;
                    }
                case "DetailCategories":
                    {
                        DetailCategory ii = (DetailCategory)ob;
                        dbProvider.DetailCategories.DeleteOnSubmit(ii);
                        break;
                    }
                case "DetailCustomers":
                    {
                        DetailCustomer ii = (DetailCustomer)ob;
                        dbProvider.DetailCustomers.DeleteOnSubmit(ii);
                        break;
                    }
                case "DetailImages":
                    {
                        DetailImage ii = (DetailImage)ob;
                        dbProvider.DetailImages.DeleteOnSubmit(ii);
                        break;
                    }
                case "DetailInfos":
                    {
                        DetailInfo ii = (DetailInfo)ob;
                        dbProvider.DetailInfos.DeleteOnSubmit(ii);
                        break;
                    }
                case "DetailUsers":
                    {
                        DetailUser ii = (DetailUser)ob;
                        dbProvider.DetailUsers.DeleteOnSubmit(ii);
                        break;
                    }
                case "Emails":
                    {
                        Email ii = (Email)ob;
                        dbProvider.Emails.DeleteOnSubmit(ii);
                        break;
                    }
                case "ErrorMessages":
                    {
                        ErrorMessage ii = (ErrorMessage)ob;
                        dbProvider.ErrorMessages.DeleteOnSubmit(ii);
                        break;
                    }
                case "Images":
                    {
                        Image ii = (Image)ob;
                        dbProvider.Images.DeleteOnSubmit(ii);
                        break;
                    }
                case "ImageLightboxes":
                    {
                        ImageLightbox ii = (ImageLightbox)ob;
                        dbProvider.ImageLightboxes.DeleteOnSubmit(ii);
                        break;
                    }
                case "Infos":
                    {
                        Info ii = (Info)ob;
                        dbProvider.Infos.DeleteOnSubmit(ii);
                        break;
                    }
                case "Languages":
                    {
                        Language ii = (Language)ob;
                        dbProvider.Languages.DeleteOnSubmit(ii);
                        break;
                    }
                case "Lightboxes":
                    {
                        Lightbox ii = (Lightbox)ob;
                        dbProvider.Lightboxes.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogCategories":
                    {
                        LogCategory ii = (LogCategory)ob;
                        dbProvider.LogCategories.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogCustomers":
                    {
                        LogCustomer ii = (LogCustomer)ob;
                        dbProvider.LogCustomers.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogEmails":
                    {
                        LogEmail ii = (LogEmail)ob;
                        dbProvider.LogEmails.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogImages":
                    {
                        LogImage ii = (LogImage)ob;
                        dbProvider.LogImages.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogImageCategories":
                    {
                        LogImageCategory ii = (LogImageCategory)ob;
                        dbProvider.LogImageCategories.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogImageLightboxes":
                    {
                        LogImageLightbox ii = (LogImageLightbox)ob;
                        dbProvider.LogImageLightboxes.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogInfos":
                    {
                        LogInfo ii = (LogInfo)ob;
                        dbProvider.LogInfos.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogLightboxes":
                    {
                        LogLightbox ii = (LogLightbox)ob;
                        dbProvider.LogLightboxes.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogNoticeEmails":
                    {
                        LogNoticeEmail ii = (LogNoticeEmail)ob;
                        dbProvider.LogNoticeEmails.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogPayments":
                    {
                        LogPayment ii = (LogPayment)ob;
                        dbProvider.LogPayments.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogRecentActivities":
                    {
                        LogRecentActivity ii = (LogRecentActivity)ob;
                        dbProvider.LogRecentActivities.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogRequests":
                    {
                        LogRequest ii = (LogRequest)ob;
                        dbProvider.LogRequests.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogSubscribers":
                    {
                        LogSubscriber ii = (LogSubscriber)ob;
                        dbProvider.LogSubscribers.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogTranslations":
                    {
                        LogTranslation ii = (LogTranslation)ob;
                        dbProvider.LogTranslations.DeleteOnSubmit(ii);
                        break;
                    }
                case "LogUsers":
                    {
                        LogUser ii = (LogUser)ob;
                        dbProvider.LogUsers.DeleteOnSubmit(ii);
                        break;
                    }
                case "NoticeEmails":
                    {
                        NoticeEmail ii = (NoticeEmail)ob;
                        dbProvider.NoticeEmails.DeleteOnSubmit(ii);
                        break;
                    }
                case "Payments":
                    {
                        Payment ii = (Payment)ob;
                        dbProvider.Payments.DeleteOnSubmit(ii);
                        break;
                    }
                case "Permissions":
                    {
                        Permission ii = (Permission)ob;
                        dbProvider.Permissions.DeleteOnSubmit(ii);
                        break;
                    }
                case "Objects":
                    {
                        Object ii = (Object)ob;
                        dbProvider.Objects.DeleteOnSubmit(ii);
                        break;
                    }
                case "Status":
                    {
                        Status ii = (Status)ob;
                        dbProvider.Status.DeleteOnSubmit(ii);
                        break;
                    }
                case "Subscribers":
                    {
                        Subscriber ii = (Subscriber)ob;
                        dbProvider.Subscribers.DeleteOnSubmit(ii);
                        break;
                    }
                case "Users":
                    {
                        User ii = (User)ob;
                        dbProvider.Users.DeleteOnSubmit(ii);
                        break;
                    }
            }

            dbProvider.SubmitChanges();

            if (TrySaveChange(dbProvider, 5))
            {
                VietImageDB_Log.WriteLog("Successed!");
                VietImageDB_Log.WriteLog("End Delete.");
                return true;
            }
            VietImageDB_Log.WriteLog("Failed!");
            VietImageDB_Log.WriteLog("End Delete.");
            return false;
        }

        protected static bool TrySaveChange(VietImageDataClassesDataContext context, int num)
        {
            if (num == 0)
                return false;
            using (context)
            {
                //VietImageDB_Log.WriteLog("Try save changes");
                try
                {
                    try
                    {
                        //Thực thi lệnh
                        context.SubmitChanges();
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, null);
                        String mess = "OptimisticConcurrencyException handled and try save changes again.";
                        //VietImageDB_Log.WriteLog(mess);
                        Thread.Sleep(10000);
                        return TrySaveChange(context, num - 1);
                    }
                }
                catch (UpdateException ex)
                {
                    String mess = ex.Message;
                    //VietImageDB_Log.WriteLog(mess);
                    return TrySaveChange(context, num - 1);
                }
            }
            return true;
        }
    }
}